import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:myapp/app_cache.dart';
import 'package:myapp/http/sample/cmd/app_login_request.dart';
import 'package:myapp/http/sample/cmd/app_login_result.dart';

class LoginController extends GetxController {
  Future<void> login(String username, String password) async {
    var response = await AppLoginRequest()
        .setParams(username, password)
        .useLoading()
        .useDefaultErr()
        .execute();
    if (response.isOk()) {
      saveUserInfo(response.data!, username);
      goMain();
    }
  }

  void saveUserInfo(AppLoginResult appLoginResult, String username) {
    AppCache.get().saveTokenAndRole(appLoginResult.accessToken!, username);
  }

  void goMain() {
    Get.offAllNamed('/home');
  }
}
